﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using JsonTest1.Data;

namespace JsonTest1.Net
{
    public class HttpFile:IRevocable
    {
        public event HttpFileCallback OnFileLoadCompleted;

        public event HttpProgressChangedCalback OnProgressChanged;

        public event HttpDataStreamCalback OnFileStreamCallback;

        public event HttpFailCalback OnFileLoadFailed;

        public event Action ProcessCompleted;

        HttpEngine mHttpGet;
        private string rqtUrl;

        public HttpFile()
        {
            mHttpGet = new HttpEngine();
            mHttpGet.OnDataStreamGenerated += ((stream, data) =>
            {
              //  BitmapSource bs = new BitmapImage();
              //  bs.SetSource(stream);
              //  if (OnFileLoadCompleted != null)
              //  {
              //      OnFileLoadCompleted(bs);
              //  }

                if (OnFileStreamCallback != null)
                {
                    OnFileStreamCallback(stream, new ResultData(0, 0, rqtUrl));
                }
            });

            mHttpGet.ProcessCompleted += (() =>
            {
                if (ProcessCompleted != null)
                {
                    ProcessCompleted();
                }
            });

            mHttpGet.OnProgressChanged += ((progress, totalProgress, type) =>
            {
                OnProgressChanged(progress, totalProgress, type);
            });

            mHttpGet.OnGetFailEvent += ((failType, errorType, data) =>
            {
                OnFileLoadFailed(failType, errorType,data);
            });
        }

        public void BeginLoadFile(String url)
        {
            this.rqtUrl = url;
            mHttpGet.BeginRequest(url);
        }

        public void RevokeAsync()
        {
            mHttpGet.RevokeAsync();
        }
    }
}
